package com.azlogica.control;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.net.SocketException;
import java.net.UnknownHostException;

import android.os.AsyncTask;

public class Connection extends AsyncTask<Void, Void, Void> {

	String dstAddress;
	int dstPort;
	String frame;
	private static int UDP_SERVER_PORT=11000;
	public Connection(String addr, int port, String frame){
		dstAddress = addr;
		dstPort = port;
		this.frame = frame;
	}
	
	@Override
	protected Void doInBackground(Void... params) {
		    DatagramSocket ds = null;
		    try {
		        ds = new DatagramSocket();
		        InetAddress serverAddr = InetAddress.getByName("10.0.2.2");
		        DatagramPacket dp;
		        dp = new DatagramPacket(frame.getBytes(), frame.length(), serverAddr, UDP_SERVER_PORT);
		        ds.send(dp);
		    } catch (SocketException e) {
		        e.printStackTrace();
		    }catch (UnknownHostException e) {
		        e.printStackTrace();
		    } catch (IOException e) {
		        e.printStackTrace();
		    } catch (Exception e) {
		        e.printStackTrace();
		    } finally {
		        if (ds != null) {
		            ds.close();
		        }
		    }
		    return null;

	}
	
}
